/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.rpc.xuelang;

import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.json.utils.JsonUtils;
import com.comac.ins.isfd.rpc.xuelang.bo.XueLangRenderBo;
import com.comac.ins.isfd.rpc.xuelang.bo.XueLangRenderResultBo;
import com.comac.ins.isfd.rpc.xuelang.bo.XueLangResponseBo;
import com.comac.ins.isfd.rpc.xuelang.config.XueLangRequestConfig;
import com.comac.ins.isfd.rpc.xuelang.constants.XueLangConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class XueLangRpcUtils {

    @Autowired
    private XueLangRestTemplateUtils xueLangRestTemplateUtils;

    @Autowired
    private XueLangRequestConfig xueLangRequestConfig;

    public String render(XueLangRenderBo xueLangRenderBo) {
        String apiUrl = xueLangRequestConfig.getPreUrl() + XueLangConstants.XUE_LANG_API_RENDER_API;
        XueLangResponseBo xueLangResponseBo = xueLangRestTemplateUtils.post(apiUrl, xueLangRenderBo);
        if (xueLangResponseBo.getData() == null) {
            log.error("调用雪浪渲染接口返回的data值为空,apiUrl：{}, 入参为：{}", apiUrl, JsonUtils.toJsonString(xueLangRenderBo));
            throw new BaseException("数据库建模后，获取数据库建模配置的data值为空");
        }
        XueLangRenderResultBo xueLangRenderResultBo = JsonUtils.parseObject(xueLangResponseBo.getData(), XueLangRenderResultBo.class);
        if (xueLangRenderResultBo == null) {
            log.error("调用雪浪渲染接口返回的data值为空,apiUrl：{}, 入参为：{}", apiUrl, JsonUtils.toJsonString(xueLangRenderBo));
            throw new BaseException("数据库建模后，获取数据库建模配置的data值为空");
        }
        String renderUrl = xueLangRenderResultBo.getRenderUrl();
        if (StringUtils.isBlank(renderUrl)) {
            log.error("调用雪浪渲染接口返回的renderUrl值为空,apiUrl：{}, 入参为：{}", apiUrl, JsonUtils.toJsonString(xueLangRenderBo));
            throw new BaseException("数据库建模后，获取数据库建模配置的data值为空");
        }
        return renderUrl;
    }
}
